<template>
  <div class="app-container">
    <el-form ref="form" :model="eduTeacher" label-width="120px">
      <el-form-item label="讲师姓名">
        <el-input v-model="eduTeacher.name"></el-input>
      </el-form-item>
      <el-form-item label="讲师排序">
        <el-input v-model="eduTeacher.sort" controls-position="right" min="0"></el-input>
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-select v-model="eduTeacher.level" clearable placeholder="请选择讲师头衔">
          <el-option label="高级讲师" :value="1"></el-option>
          <el-option label="首席讲师" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="讲师资历">
        <el-input v-model="eduTeacher.career"></el-input>
      </el-form-item>
      <el-form-item label="讲师简介">
        <el-input type="textarea" v-model="eduTeacher.intro" :rows="10"></el-input>
      </el-form-item>
      <!-- 讲师头像 -->
      <el-form-item label="讲师头像">

          <!-- 头衔缩略图 -->
          <pan-thumb :image="eduTeacher.avatar"/>
          <!-- 文件上传按钮 -->
          <el-button type="primary" icon="el-icon-upload" @click="imagecropperShow=true">更换头像
          </el-button>

          <!--
      v-show：是否显示上传组件
      :key：类似于id，如果一个页面多个图片上传控件，可以做区分
      :url：后台上传的url地址
      @close：关闭上传组件
      @crop-upload-success：上传成功后的回调 
        <input type="file" name="file"/>
      -->
          <image-cropper
                        v-show="imagecropperShow"
                        :width="200"
                        :height="200"
                        :key="imagecropperKey"
                        :url="BASE_API+'/eduoss/fileoss'"
                        field="file"
                        @close="close"
                        @crop-upload-success="cropSuccess"/>
      </el-form-item>

      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate()">确定</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import teacher from "@/api/edu/teacher";
import ImageCropper from '@/components/ImageCropper'
import PanThumb from '@/components/PanThumb'

export default {
  components: { ImageCropper, PanThumb },
  data() {
    return {
      eduTeacher: {
        name: "",
        sort: 0,
        level: 1,
        career: "",
        intro: "",
        //设置个默认头像
        avatar: "https://edu-ls-1010.oss-cn-beijing.aliyuncs.com/2021/11/20/03cf67180ce243bcae320935a289246004.jpg"
      },
      saveBtnDisabled: false, //保存按钮是否禁用
      //上传弹框组件是否显示
      imagecropperShow:false,
      imagecropperKey:0,//上传组件key值
      BASE_API:process.env.VUE_APP_BASE_API, //获取地址
    };
  },
  //用于跳转到修改页面
  created() {
    //根据路由是否有id判断
    if (this.$route.params && this.$route.params.id) {
      const id = this.$route.params.id;
      this.getTeacherInfo(id);
    }
  },
  methods: {
    saveOrUpdate() {
      if (!this.eduTeacher.id) {
        this.save();
      } else {
        this.update();
      }
    },
    //添加讲师
    save() {
      teacher
        .saveTeacher(this.eduTeacher)
        .then(response => {
          this.$message({
            type: "success",
            message: "添加成功!"
          }); //路由跳转
          this.$router.push({ path: "/teacher/table" });
        })
        .catch(error => {
          console.log(error);
        });
    },
    //修改讲师
    update(eduTeacher) {
      teacher
        .updateTeacher(this.eduTeacher)
        .then(response => {
          this.$message({
            type: "success",
            message: "修改成功!"
          }); //路由跳转
          this.$router.push({ path: "/teacher/table" });
        })
        .catch(error => {
          console.log(error);
        });
    },
    //获取讲师信息
    getTeacherInfo(id) {
      teacher
        .getTeacherById(id)
        .then(response => {
          this.eduTeacher = response.data.teacher;
        })
        .catch(error => {
          console.log(error);
        });
    },
    //用于头像上传
    close() { //关闭上传弹框的方法
        this.imagecropperShow=false
        //上传组件初始化
        this.imagecropperKey = this.imagecropperKey+1
    },
    //上传成功方法
    cropSuccess(data) {
      this.imagecropperShow=false
      //上传之后接口返回图片地址
      this.eduTeacher.avatar = data.url
      this.imagecropperKey = this.imagecropperKey+1
    },

  }
};
</script>